//
//  AppDelegate.m
//  HWPromoteProjectAPP
//
//  Created by 陈道斌 on 2020/1/19.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "AppDelegate.h"
#import "WXApi.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import "HWTabBarController.h"
#import "HWLoginViewController.h"
#import "HWPromoteProjectAPP-Swift.h"
#define   WXAPP_ID   @""
@interface AppDelegate ()<TencentSessionDelegate>
/** QQ分享*/
@property(nonatomic, strong) TencentOAuth * tencentOAuth;

/** 许可*/
@property(nonatomic, strong) NSArray * permissions;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
   
    
    
    HWLoginViewController *login = [[HWLoginViewController alloc]init];
    
    
    if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
        
        
        self.window = [[UIWindow alloc] init];
        self.window.backgroundColor = [UIColor whiteColor];
        self.window.frame = [UIScreen mainScreen].bounds;
        [self.window makeKeyAndVisible];
        self.window.rootViewController = login;
        
    } else {
        
        HWTabBarController1 *viewcontr1 = [[HWTabBarController1 alloc]init];
        self.window = [[UIWindow alloc] init];
        self.window.backgroundColor = [UIColor whiteColor];
        self.window.frame = [UIScreen mainScreen].bounds;
        [self.window makeKeyAndVisible];
        self.window.rootViewController = viewcontr1;
        
        
    }
 
    
      [WXApi registerApp:WXAPP_ID enableMTA:false];
    
//
//     [WXApi registerApp:@“”
//    universalLink:UNIVERSAL_LINK];
//
       //可以不写
     //  _tencentOAuth.redirectURI = @"www.qq.com";
//       
   //  _permissions = [NSArray arrayWithObjects:@"get_user_info",@"get_simple_userinfo", @"add_t",nil];
//       
//       [_tencentOAuth authorize:_permissions inSafari:YES];
    
   // [_tencentOAuth authorize:_permissions inSafari:NO];
     _tencentOAuth = [[TencentOAuth alloc]initWithAppId:@"" andDelegate:nil];
     _tencentOAuth.redirectURI = @"www.qq.com";
    //  _permissions = [NSArray arrayWithObjects:@"get_user_info",@"get_simple_userinfo", @"add_t", nil];
      
//      [_tencentOAuth authorize:_permissions inSafari:NO];
    
    
    
    

    return YES;
}


#pragma mark - UISceneSession lifecycle






//- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
//{
//    if ([url.host isEqualToString:@"safepay"]) {
//        // 支付跳转支付宝钱包进行支付，处理支付结果
//
//
//    }
//    return YES;
//}
//



//- (BOOL)application:(UIApplication *)application
//            openURL:(NSURL *)url
//  sourceApplication:(NSString *)sourceApplication
//         annotation:(id)annotation {
//
//    if ([url.host isEqualToString:@"safepay"]) {
////        //跳转支付宝钱包进行支付，处理支付结果
////        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
////            NSLog(@"result = %@",resultDic);
////        }];
//    }
//    return YES;
//
//
//}
//
//// NOTE: 9.0以后使用新API接口
//- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
//{
//    if ([url.host isEqualToString:@"safepay"]) {
//        //跳转支付宝钱包进行支付，处理支付结果
////        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
////            NSLog(@"result = %@",resultDic);
////        }];
//    }
//    return YES;
//}





//回到App时该回调有走
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    
    [WXApi handleOpenURL:url delegate:self];
    return [TencentOAuth HandleOpenURL:url];
}


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    
     [WXApi handleOpenURL:url delegate:self];
    return [TencentOAuth HandleOpenURL:url];
}


- (void)onReq:(BaseReq *)req
{
     NSLog(@"onReq是微信终端向第三方程序发起请求，要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时，会切回到微信终端程序界面");
}


- (void)onResp:(BaseResp *)resp
{
NSLog(@"回调处理");

// 处理 分享请求 回调
//if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
//    switch (resp.errCode) {
//        case WXSuccess:
//        {
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
//                                                            message:@"分享成功!"
//                                                           delegate:self
//                                                  cancelButtonTitle:@"OK"
//                                                  otherButtonTitles:nil, nil];
//            [alert show];
//        }
//            break;
//
//        default:
//        {
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
//                                                            message:@"分享失败!"
//                                                           delegate:self
//                                                  cancelButtonTitle:@"OK"
//                                                  otherButtonTitles:nil, nil];
//            [alert show];
//        }
//            break;
//    }
//  }
}



//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
//    return  [WXApi handleOpenURL:url delegate:self];
//}
//
//- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
//    return [WXApi handleOpenURL:url delegate:self];
//}


/*
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
#if __QQAPI_ENABLE__
    [QQApiInterface handleOpenURL:url delegate:(id)[QQAPIDemoEntry class]];
#endif
    if (YES == [TencentOAuth CanHandleOpenURL:url])
    {
        return [TencentOAuth HandleOpenURL:url];
    }
    return YES;
}
*/


//登录功能没添加，但调用TencentOAuth相关方法进行分享必须添加<TencentSessionDelegate>，则以下方法必须实现，尽管并不需要实际使用它们
//登录成功
- (void)tencentDidLogin
{
//    _labelTitle.text = @"登录完成";
    if (_tencentOAuth.accessToken && 0 != [_tencentOAuth.accessToken length])
    {
        // 记录登录用户的OpenID、Token以及过期时间
//        _labelAccessToken.text = _tencentOAuth.accessToken;
    }
    else
    {
//        _labelAccessToken.text = @"登录不成功 没有获取accesstoken";
    }
}


//非网络错误导致登录失败
-(void)tencentDidNotLogin:(BOOL)cancelled
{
    if (cancelled)
    {
//        _labelTitle.text = @"用户取消登录";
    }
    else
    {
//        _labelTitle.text = @"登录失败";
    }
}

//网络错误导致登录失败
-(void)tencentDidNotNetWork
{
//    _labelTitle.text=@"无网络连接，请设置网络";
}

@end
